package global

import (
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

var SystemConfig *Config

type Config struct {
	Server        ServerCfg     `yaml:"Server"`
	DB            MySQLCfg      `yaml:"DB"`
	Redis         RedisCfg      `yaml:"Redis"`
	Env           string        `yaml:"Env"` // 环境变量 env  online 和 dev
	Consul        ConsulCfg     `yaml:"Consul"`
	LocalIP       string        `yaml:"LocalIP"`
	ZipKinService ZipKinService `yaml:"ZipKinService"`
}

type ServerCfg struct {
	ServerHttpPort string `yaml:"serverHttpPort"`
	ServerGrpcPort string `yaml:"serverGrpcPort"`
	LogDir         string `yaml:"logDir"`
	LogLevel       string `yaml:"logLevel"`
}

type MySQLCfg struct {
	UserName   string `yaml:"userName"`
	UserPasswd string `yaml:"userPasswd"`
	DbIP       string `yaml:"dbIP"`
	DbPort     int    `yaml:"dbPort"`
	DbName     string `yaml:"dbName"`
	DbLogDir   string `yaml:"dbLogDir"`
	LogModel   bool   `yaml:"logModel"`
}

type RedisCfg struct {
	Host    string `yaml:"host"`
	Auth    string `yaml:"auth"`
	RedisDb int    `yaml:"redisDb"`
}

type ConsulCfg struct {
	ServiceName string `yaml:"serviceName"`
	IP          string `yaml:"ip"`
	Port        string `yaml:"port"`
}

type ZipKinService struct {
	IP   string `yaml:"ip"`
	Port string `yaml:"port"`
}

// LoadConfig 载入配置文件
func LoadConfig(configFile string) error {
	// 读取 yaml 配置文件
	c, err := ioutil.ReadFile(configFile)
	ret := Config{}
	err = yaml.Unmarshal(c, &ret)
	SystemConfig = &ret
	return err
}
